package com.talent.controller;

import com.talent.entities.dto.VaccOrder;
import com.talent.entities.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;

/**
 * @author 周意朗
 * Date: 2022/2/23
 * @since JDK 11
 */
@RestController
@Slf4j
@EnableAutoConfiguration
@CrossOrigin
public class VaccOrderController {
    @Resource
    private RestTemplate restTemplate;

    @Value("${service-url.vacc-user-service}")
    private String serverURL;

    @GetMapping(value = "/consumer/payment/{id}")
    public String getReady(@PathVariable("id") Long id, HttpServletRequest request, Model model, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        return restTemplate.getForObject(serverURL + "/payment/" + id, String.class);
    }

    @PostMapping("/order/selectfavorite/{id}")
    public CommonResult<LinkedList> selectFavorite(@PathVariable("id") String userid,
                                                   HttpServletRequest request,
                                                   Model model,
                                                   HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");

        LinkedList res = restTemplate.postForObject(serverURL + "/order/selectfavorite", userid, LinkedList.class);
        if (res == null) {
            return new CommonResult<>(400, "null", null);
        }
        return new CommonResult<>(200, "success", res);
    }

    @PostMapping("/order/addfavorite/{userid}/{vaccid}")
    public CommonResult<String> addFavorite(@PathVariable("userid") String userid,
                                            @PathVariable("vaccid") String vaccid,
                                            HttpServletRequest request,
                                            Model model,
                                            HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("userid", userid);
        map.add("vaccid", vaccid);
        String res = restTemplate.postForObject(serverURL + "/order/addfavorite", map, String.class);
        if ("success".equals(res)) {
            return new CommonResult<>(200, "success", res);
        }
        return new CommonResult<>(400, "fail", res);
    }


    @PostMapping("/order/deletefavorite/{userid}/{vaccid}")
    public CommonResult<String> deleteFavorite(@PathVariable("userid") String userid,
                                               @PathVariable("vaccid") String vaccid,
                                               HttpServletRequest request,
                                               Model model,
                                               HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("userid", userid);
        map.add("vaccid", vaccid);
        String res = restTemplate.postForObject(serverURL + "/order/deletefavorite", map, String.class);
        if ("success".equals(res)) {
            return new CommonResult<>(200, "success", res);
        }
        return new CommonResult<>(400, "fail", res);
    }

    @PostMapping("/order/createorder/")
    public CommonResult<String> createOder(@RequestBody VaccOrder vaccOrder,
                                           HttpServletRequest request,
                                           Model model,
                                           HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        String res = restTemplate.postForObject(serverURL + "/order/createorder/", vaccOrder, String.class);
        if (res != null && res.length() != 0) {
            return new CommonResult<String>(200, "success", res);
        }
        return new CommonResult<String>(400, "fail", null);
    }

    @PostMapping("/order/finishorder/{id}")
    public CommonResult<String> finishOrder(@PathVariable("id") String id,
                                            HttpServletRequest request,
                                            Model model,
                                            HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        Boolean res = restTemplate.postForObject(serverURL + "/order/finishorder/", id, Boolean.class);
        if (res != null && res) {
            return new CommonResult<>(200, "success", "success");
        }
        return new CommonResult<>(400, "fail", "fail");
    }

    @PostMapping("/order/selectorder/{userid}")
    public CommonResult<LinkedList> selectOrder(@PathVariable("userid") String id,
                                            HttpServletRequest request,
                                            Model model,
                                            HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        LinkedList res = restTemplate.postForObject(serverURL + "/order/selectorder", id, LinkedList.class);
        if (res != null) {
            return new CommonResult<LinkedList>(200,"success",res);
        }
        return new CommonResult<>(400, "fail", null);
    }
}
